package com.itcast.googleplay.fragment;

import java.util.List;
import java.util.Random;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.itcast.googleplay.protocol.HotProtocol;
import com.itcast.googleplay.ui.FlowLayout;
import com.itcast.googleplay.ui.FlowLayout1;
import com.itcast.googleplay.ui.FlowLayout2;
import com.itcast.googleplay.ui.Flowlayout3;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.DrawableUtil;
import com.itcast.googleplay.utils.UIUtils;


public class HotFragment extends BaseFragment {
	private List<String> hotList;
	@Override
	public ResultState onSubLoad() {
		HotProtocol hotProtocol = new HotProtocol();
		hotList = hotProtocol.getData("hot", 0, "");
		if(hotList!=null){
			if(hotList.size()>0){
				return ResultState.STATE_SUCCESSED;
			}
			return ResultState.STATE_EMTPY;
		}
		return ResultState.STATE_ERROR;
	}
	@Override
	public View onCreateSuccessedSubView() {
		FlowLayout flowLayout = new FlowLayout(UIUtils.getContext());
//		FlowLayout1 flowLayout = new FlowLayout1(UIUtils.getContext());
//		FlowLayout2 flowLayout = new FlowLayout2(UIUtils.getContext());
//		Flowlayout3 flowLayout = new Flowlayout3(UIUtils.getContext());
		int padding = UIUtils.dip2px(10);
		flowLayout.setPadding(padding, padding, padding, padding);
		for (int i = 0; i < hotList.size(); i++) {
			TextView textView = new TextView(UIUtils.getContext());
			textView.setText(hotList.get(i));
			textView.setTextColor(Color.WHITE);
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
			textView.setGravity(Gravity.CENTER);
			
			int red = 30+new Random().nextInt(210);
			int green = 30+new Random().nextInt(210);
			int blue = 30+new Random().nextInt(210);
			
			int rgb = Color.rgb(red, green, blue);
			
			textView.setPadding(padding, padding, padding, padding);
			
			int radius = UIUtils.dip2px(10);
			
			Drawable drawableBitmapNormal = DrawableUtil.drawableBitmap(rgb,radius);
			Drawable drawableBitmapPress = DrawableUtil.drawableBitmap(Color.GRAY,radius);
			
			StateListDrawable drawableState = DrawableUtil.getDrawableState(drawableBitmapPress,drawableBitmapNormal);
			textView.setBackgroundDrawable(drawableState);
			
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(UIUtils.getContext(),"11111", 0).show();
				}
			});
			
			flowLayout.addView(textView);
		}
		return flowLayout;
	}
}
